﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using System.IO;

namespace NHA_HANG_BUFFET
{
    public partial class frmThuNgan : DevComponents.DotNetBar.Office2007Form
    {
        public frmThuNgan()
        {
            InitializeComponent();
        }

        //Khai báo property cho NhanVien
        private DTO.NhanVien nv = null;

        public DTO.NhanVien NV
        {
            get { return nv; }
            set { nv = value; }
        }

        DichVuB busDV = new DichVuB();
        TheKhachHangB busTKH = new TheKhachHangB();
        DataTable dt = new DataTable();
        VeB busV = new VeB();

        private void frmThuNgan_Load(object sender, EventArgs e)
        {
            //Hiện tên nhân viên và nút thoát
            this.lbDangXuat.ForeColor = Color.Red;
            this.lbTenNV.ForeColor = Color.Red;
            //this.lbTenNV.Text = nv.TenNV;

            timer1.Start(); 
            LayDSNuocUong();
            LayDSBuffet(2);
            LayThongTinVe();
            LayGiaVe();
        }

        private void rdbKhaiVi_CheckedChanged(object sender, EventArgs e)
        {
            LayDSBuffet(1);
        }

        private void rdbMonNuong_CheckedChanged(object sender, EventArgs e)
        {
            LayDSBuffet(2);
        }

        private void rdbMonLau_CheckedChanged(object sender, EventArgs e)
        {
            LayDSBuffet(3);
        }

        private void rdbTrangMieng_CheckedChanged(object sender, EventArgs e)
        {
            LayDSBuffet(4);
        }

        private void LivBuffer_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string madv = LivBuffer.Items[LivBuffer.FocusedItem.Index].SubItems[1].Text;
            frmThuNganThanhToan_BanVe tt = new frmThuNganThanhToan_BanVe();
            tt.lbDSDichVu.Text = "Danh sách Buffet";
            tt.lbTieuDe.Text = "BÁN VÉ";
            tt.dgvDichVuKhac.Visible = false;
            tt.dgvDSBuffet.Visible = true;
            tt.lbMaDV.Text = madv;
            tt.lbBuoi.Text = cbBuoi.Text;
            tt.lbNgayThuong.Text = cbNgayThuong.Text;
            tt.Text = lbTenNV.Text + " bán vé";
            tt.Manv = int.Parse(lbManv.Text);
            tt.lbDoiTuong.Text = cbDoiTuong.Text;
            tt.lbgiave.Text = lbDonGiaVe.Text;
            tt.ShowDialog();
        }
        private void txtTenNuocGK_TextChanged(object sender, EventArgs e)
        {
            dgvDichVuKhac.DataSource = busDV.bTimNuocUong(txtTenNuocGK.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lbNgayHT.Text = DateTime.Today.DayOfWeek.ToString() + " , " + DateTime.Now.ToShortDateString();
            lbThoiGian.Text = DateTime.Now.ToShortTimeString();
        }

        private void lbThoiGian_TextChanged(object sender, EventArgs e)
        {
            LayThongTinVe();
        }

        private void tabKHTT_Click(object sender, EventArgs e)
        {
            dgvKhachHang.DataSource = busTKH.bLayDSKHThanThiet();
        }

        private void txtTenKhachHang_TextChanged(object sender, EventArgs e)
        {
            dgvKhachHang.DataSource = busTKH.bTimKhThanThiet(txtTenKhachHang.Text);
        }

        private void dgvDichVuKhac_DoubleClick(object sender, EventArgs e)
        {
            int dong = dgvDichVuKhac.CurrentCell.RowIndex;
            string madv = dgvDichVuKhac.Rows[dong].Cells["Madichvu"].Value.ToString();
            frmThuNganThanhToan_BanVe tt = new frmThuNganThanhToan_BanVe();
            tt.lbDSDichVu.Text = "Danh sách Thức uống";
            tt.lbTieuDe.Text = "THANH TOÁN";
            tt.dgvDichVuKhac.Visible = true;
            tt.dgvDSBuffet.Visible = false;
            tt.lbBuoi.Visible = false;
            tt.lbNgayThuong.Visible = false;
            tt.lbChuThichBuoi.Visible = false;
            tt.lbMaDV.Text = madv;
      
            tt.Text = lbTenNV.Text + " thanh toán";
            tt.Manv = int.Parse(lbManv.Text);
            tt.ShowDialog();
        }
        
        public void LayDSNuocUong()
        {
            dgvDichVuKhac.DataSource = busDV.bLayDSNuocUong();
        }

        public void LayDSBuffet(int loaiBuffet)
        {
            LivBuffer.Items.Clear();
            imageList1.Images.Clear();
            dt = busDV.bLayDSBuffet(loaiBuffet);
            int nImage = 0;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                imageList1.Images.Add(byteArrayToImage((byte[])dt.Rows[i]["HinhAnh"]));
                ListViewItem viewItem = new ListViewItem(dt.Rows[nImage]["TenDV"].ToString());
                viewItem.SubItems.Add(dt.Rows[i]["MaDV"].ToString());
                viewItem.SubItems.Add(dt.Rows[nImage]["TenDV"].ToString());
                viewItem.ImageIndex = nImage;
                LivBuffer.Items.Add(viewItem);
                nImage++;
            }
        }

        private Image byteArrayToImage(byte[] p)
        {
            MemoryStream stream = new MemoryStream(p);
            Image image = Image.FromStream(stream);
            return image;
        }

        public void LayThongTinVe()
        {
            //Lấy buổi trong ngày
            string giohientai = DateTime.Now.ToString();
            if (DateTime.Compare(DateTime.Parse(giohientai), DateTime.Parse("6:00 PM")) >= 0)
            {
                cbBuoi.Text = "Tối";
            }
            else
                cbBuoi.Text = "Trưa";

            //Lấy thứ ngày trong tuần
            string thu = DateTime.Today.DayOfWeek.ToString();
            if (thu == "Satuday" || thu == "Sunday")
            {
                cbNgayThuong.Text = "Thứ 7/CN";
            }
            else
                cbNgayThuong.Text = "Ngày thường";

            //Lấy giá vé theo thời gian và đối tượng
            LayGiaVe();
        }

        public void LayGiaVe()
        {
            lbDonGiaVe.Text = busV.bLayGiaVe(cbBuoi.Text, cbNgayThuong.Text, cbDoiTuong.Text);
        }

        private void cbBuoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            LayGiaVe();
        }

        private void cbNgayThuong_SelectedIndexChanged(object sender, EventArgs e)
        {
            LayGiaVe();
        }

        private void cbDoiTuong_SelectedIndexChanged(object sender, EventArgs e)
        {
            LayGiaVe();
        }

        private void lbDangXuat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}